package com.supconit.its.environment;

import com.google.common.base.CaseFormat;
import com.google.common.base.Converter;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * <p>
 * 环境配置
 * </p>
 *
 * @author 史金涛
 * @date 2020/8/7 15:22
 */
public class Environment {

    /**
     * 作者名
     */
    public static String AUTHOR;

    /**
     * 工程目录
     */
    public static String PROJECT_PATH;

    /**
     * 模块名
     */
    public static String MODULE_NAME;

    /**
     * 表名
     */
    public static String TABLE_NAME;

    /**
     * 包名
     */
    public static String PACKAGE;

    /**
     * domain 路径
     */
    public static String DOMAIN_PATH;


    /**
     * 数据源配置
     */
    public static String URL;
    public static String DRIVER_NAME;
    public static String USERNAME;
    public static String PASSWORD;

    /* UserName -> userName */
    public static final Converter<String, String> UPPER_CAMEL2LOWER_CAMEL_CONVERTER =
            CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL);

    /* userName -> UserName */
    public static final Converter<String, String> LOWER_CAMEL2UPPER_CAMEL_CONVERTER =
            CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL);

    /* user_name -> userName */
    public static final Converter<String, String> LOWER_UNDERSCORE2LOWER_CAMEL_CONVERTER =
            CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.LOWER_CAMEL);

    /* userName -> user_name */
    public static final Converter<String, String> LOWER_CAMEL2LOWER_UNDERSCORE_CONVERTER =
            CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE);

    /* entityName */
    public static String entityName;

    /* 映射出的实体名 */
    public static String MAPPED_ENTITY_NAME;

    /* EntityName  */
    public static String entityUpCase;

    /* 路径分隔符号 */
    public static String separator;

    /* maven source */
    public static String mavenSource;

    /* 项目路径 */
    public static String USER_DIR;

    /**
     * 初始化
     */
    public static void init() {
        System.out.println("Environment 初始化...");
        Properties properties = new Properties();
        InputStream inputStream = Environment.class.getResourceAsStream("/application.properties");
        try {
            properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            separator = File.separator;
            USER_DIR = System.getProperty("user.dir");
            mavenSource = "src/main/java/".replace("/", separator);
            AUTHOR = properties.getProperty("generator.author");
            PROJECT_PATH = properties.getProperty("generator.project_path");
            if (StringUtils.isBlank(PROJECT_PATH)) {
                PROJECT_PATH = USER_DIR + separator;
            }
            MODULE_NAME = properties.getProperty("generator.module_name");
            PACKAGE = properties.getProperty("generator.package_name");
            TABLE_NAME = properties.getProperty("generator.table_name");
            String mappedEntityName = properties.getProperty("generator.mapped_entity_name");
            if (StringUtils.isNotBlank(mappedEntityName)) {
                MAPPED_ENTITY_NAME = mappedEntityName.substring(0, 1).toUpperCase() + mappedEntityName.substring(1);
            }
            URL = properties.getProperty("spring.datasource.url");
            DRIVER_NAME = properties.getProperty("spring.datasource.driver-class-name");
            USERNAME = properties.getProperty("spring.datasource.username");
            PASSWORD = properties.getProperty("spring.datasource.password");
            DOMAIN_PATH = PROJECT_PATH + mavenSource + PACKAGE.replace(".", separator) +
                    separator + MODULE_NAME + separator + "domain" + separator;
            entityName = LOWER_UNDERSCORE2LOWER_CAMEL_CONVERTER.convert(Environment.TABLE_NAME);
            entityUpCase = LOWER_CAMEL2UPPER_CAMEL_CONVERTER.convert(entityName);
            System.out.println("Environment 初始化完成");
        } catch (IOException e) {
            System.out.println("Environment 初始化失败");
            e.printStackTrace();
        }
    }

}
